package com.springboot.admin.service.impl;

import com.springboot.admin.entity.AccountDto;
import com.springboot.admin.entity.SiteInfoDto;
import com.springboot.admin.jwt.JwtAuthService;
import com.springboot.admin.mail.MailSenderBoot;
import com.springboot.admin.mapper.AccountDao;
import com.springboot.admin.security.service.GetCurrentLoginUsername;
import com.springboot.admin.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {

  @Autowired
  AccountDao accountDao;
  @Autowired
  JwtAuthService jwtAuthService;
  @Autowired
  MailSenderBoot mailSenderBoot;
  @Autowired
  GetCurrentLoginUsername getCurrentLoginUsername;

  public AccountDto login(AccountDto accountDto ) throws Exception {

    String username = accountDto.getUsername();
    String password = accountDto.getPassword();
    if (username.isEmpty() || password.isEmpty()){
      throw new Exception("用户密码不能为空");
    }
    String  token = jwtAuthService.login(username,password);
    AccountDto user = accountDao.getUserByname(username);
    String roleListjoin = getRoleList(username);

    // 从数据库获取站长邮箱地址发送通知邮件
    Map siteInfo = accountDao.getSiteInfo();

    // 非管理员发送登录邮件通知
    if (!username.equals("admin")){
      mailSenderBoot.sendMail((String)siteInfo.get("siteEmail"), username + "登录了系统", "登录通知");
    }

    // 写入登入日志
    insertLoginLog(username);

    user.setRole(roleListjoin);
    user.setToken(token);
    user.setPassword("");

    return user;
  }

  public AccountDto getUserByname(String name) {
    return accountDao.getUserByname(name);
  }

  /**
   * 更新用户信息
   * @param accountDto
   * @return
   */
  public void updateUserInfo(AccountDto accountDto) {
    String username = getCurrentLoginUsername.getUsername();
    accountDto.setUsername(username);
    accountDao.updateUserInfo(accountDto);
  }

  public String getRoleList(String username) {
    // 动态获取权限
    List<Map> roleList = accountDao.getRolesByUserName(username);

    String roleListjoin = "";
    for (int i =0; i<roleList.size();i++){
      if (i != (roleList.size() -1)){
        roleListjoin += roleList.get(i).get("roleId") + ",";
      } else {
        roleListjoin += roleList.get(i).get("roleId");
      }
    }
    return roleListjoin;
  }

  public List<Map> getUserList(int page) {
    List<Map> userList = accountDao.getUserList(page);
    for (Map user: userList){
      user.put("role", getRoleList((String)user.get("username")));
    }
    return userList;
  }

  public List<Map> getRoleList() {
    List<Map> userList = accountDao.getRoleList();
    for (Map user: userList){
      user.put("role", getRoleList((String)user.get("username")));
    }
    return userList;
  }

  public void insertRole(String username,String roleId) {
    accountDao.insertRole(username,roleId);
  }

  public void deleteRolesByUsername(String username) {
    accountDao.deleteRolesByUsername(username);
  }

  public void insertUser(String username,String password,String email) {
    accountDao.insertUser(username,password,email);
    // 新注册用户默认给作者权限
    accountDao.insertRole(username,"ROLE_author");
  }

  public void deleUser(int id, String username) {
    accountDao.deleUser(id);
    accountDao.deleUserRoles(username);
  }

  public Map getSiteInfo() {
    return accountDao.getSiteInfo();
  }

  public AccountDto getUserInfo() {

    String username = getCurrentLoginUsername.getUsername();
    AccountDto user = accountDao.getUserByname(username);
    String roleListjoin = getRoleList(username);
    user.setRole(roleListjoin);

    if (user !=null){
      user.setPassword("");
    }
    return  user;
  }

  public void updateSiteInfo(SiteInfoDto siteInfoDto) {
    accountDao.updateSiteInfo(siteInfoDto);
  }

  public void insertLoginLog(String username) {
    accountDao.insertLoginLog(username);
  }

  public void register(Map map) {
    String username = (String) map.get("username");
    String password = (String) map.get("password");
    String email = (String) map.get("email");
    accountDao.insertUser(username,password,email);
    // 新注册用户默认给作者权限
    accountDao.insertRole(username,"ROLE_author");
    Map siteInfo = accountDao.getSiteInfo();
    mailSenderBoot.sendMail((String)siteInfo.get("siteEmail"), username + "注册了账号", "注册通知");
  }
}